/* eslint-disable */
/*
 * @Description: 表单验证
 * @Author:
 * @Date:
 * @LastEditors: dh
 */

// 邮箱
var validEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
// 邮箱 /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
/* 整数或小数 */
var inDec = /^-?[0-9]+(\.?[0-9]+)?$/;
// 数字
var number = /\d+/;
// 移动电话号码
var mobilePhone = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
// 固定电话号码
var telephone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
// 域名url
var url = /^http[s]?:\/\/.*/;
// url /^(https?:|mailto:|tel:)/
// ip地址
var validURL = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/;
// ip /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$//^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
// 空白字符
var blank = /^\S*$/;
// 身份证号
var IDCard =
	/^[1-9][0-7]\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$/;
// 中文
var chinese = /^[^\u4e00-\u9fa5]+$/;
//  特殊字符
var special = /[`~!@#$%^&*()_\-+=<>?:"{}|,;；./'\\[\]·~！@#￥%……&*（）——\|《》？：“”【】、‘’，。、]/;
// 严格的身份证校验
const isCardID = sId => {
	if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
		console.log("你输入的身份证长度或格式错误");
		return false;
	}
	//身份证城市
	var aCity = {
		11: "北京",
		12: "天津",
		13: "河北",
		14: "山西",
		15: "内蒙古",
		21: "辽宁",
		22: "吉林",
		23: "黑龙江",
		31: "上海",
		32: "江苏",
		33: "浙江",
		34: "安徽",
		35: "福建",
		36: "江西",
		37: "山东",
		41: "河南",
		42: "湖北",
		43: "湖南",
		44: "广东",
		45: "广西",
		46: "海南",
		50: "重庆",
		51: "四川",
		52: "贵州",
		53: "云南",
		54: "西藏",
		61: "陕西",
		62: "甘肃",
		63: "青海",
		64: "宁夏",
		65: "新疆",
		71: "台湾",
		81: "香港",
		82: "澳门",
		91: "国外",
	};
	if (!aCity[parseInt(sId.substr(0, 2))]) {
		console.log("你的身份证地区非法");
		return false;
	}

	// 出生日期验证
	var sBirthday = (sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2))).replace(/-/g, "/"),
		d = new Date(sBirthday);
	if (sBirthday != d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()) {
		console.log("身份证上的出生日期非法");
		return false;
	}

	// 身份证号码校验
	var sum = 0,
		weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
		codes = "10X98765432";
	for (var i = 0; i < sId.length - 1; i++) {
		sum += sId[i] * weights[i];
	}
	var last = codes[sum % 11]; //计算出来的最后一位身份证号码
	if (sId[sId.length - 1] != last) {
		console.log("你输入的身份证号非法");
		return false;
	}

	return true;
};

// normal不能为空不能包含特殊字符不能有空格
const expnoraml = params => {
	var normal = (rule, value, callback) => {
		if (value) {
			if (special.test(value)) {
				return callback(new Error(params + "不能包含特殊字符"));
			} else if (!blank.test(value)) {
				return callback(new Error(params + "不能包含空格"));
			} else {
				callback();
			}
		} else {
			callback();
		}
	};
	return normal;
};

export const isIp = (rule, value, callback) => {
	if (value) {
		if (!ip.test(value)) {
			return callback(new Error("ip地址格式不正确"));
		} else {
			callback();
		}
	} else {
		callback();
	}
};
// 身份证验证
var isIDCard = (rule, value, callback) => {
	if (value) {
		if (!IDCard.test(Number(value))) {
			return callback(new Error("请输入正确的身份证号码"));
		} else {
			callback();
		}
	} else {
		callback();
	}
};
// 手机号验证
var isPhone = (rule, value, callback) => {
	if (value) {
		if (!telephone.test(value)) {
			return callback(new Error("请输入正确的手机号"));
		} else {
			callback();
		}
	} else {
		callback();
	}
};

// 邮箱验证
var isEmail = (rule, value, callback) => {
	if (value) {
		if (!email.test(value)) {
			return callback(new Error("邮箱格式不正确"));
		} else {
			callback();
		}
	} else {
		callback();
	}
};

const pwdReg = /[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/;
// 密码必须包含数字和字母组合
var passwordStrengthValidator = (rule, value, callback) => {
	if (!value) {
		callback();
	} else if (!pwdReg.test(value)) {
		callback(new Error("密码必须包含数字和字母组合"));
	} else {
		callback();
	}
};

// 是否是微信浏览器
export const isWeiXin = () => {
	return ua.match(/microMessenger/i) == "micromessenger";
};

// 是否是移动端
export const isDeviceMobile = () => {
	return /android|webos|iphone|ipod|balckberry/i.test(ua);
};

// 是否ios
export const isIos = () => {
	var u = navigator.userAgent;
	if (u.indexOf("Android") > -1 || u.indexOf("Linux") > -1) {
		//安卓手机
		return false;
	} else if (u.indexOf("iPhone") > -1) {
		//苹果手机
		return true;
	} else if (u.indexOf("iPad") > -1) {
		//iPad
		return false;
	} else if (u.indexOf("Windows Phone") > -1) {
		//winphone手机
		return false;
	} else {
		return false;
	}
};

// 是否为PC端
export const isPC = () => {
	var userAgentInfo = navigator.userAgent;
	var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = false;
			break;
		}
	}
	return flag;
};

// 判断是否是url
export function isUrl(url) {
	const a = document.createElement("a");
	a.href = url;
	return [/^(http|https):$/.test(a.protocol), a.host, a.pathname !== url, a.pathname !== `/${url}`].find(x => !x) === undefined;
}

// 判断是否是身份证号
export const isIdCard = function (cardid) {
	// 身份证正则表达式(18位)
	const isIdCard2 = /^[1-9]\d{5}(19\d{2}|[2-9]\d{3})((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])(\d{4}|\d{3}X)$/i;
	const stard = "10X98765432"; // 最后一位身份证的号码
	const first = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 1-17系数
	let sum = 0;
	if (!isIdCard2.test(cardid)) {
		return false;
	}
	const year = cardid.substr(6, 4);
	const month = cardid.substr(10, 2);
	const day = cardid.substr(12, 2);
	const birthday = cardid.substr(6, 8);
	let formatDate = new Date(year + "/" + month + "/" + day);
	if (formatDate instanceof Date) {
		const year = formatDate.getFullYear();
		let month = formatDate.getMonth() + 1;
		month = month < 10 ? "0" + month : month;
		let day = formatDate.getDate();
		day = day < 10 ? "0" + day : day;
		formatDate = year.toString() + month.toString() + day.toString();
	}

	if (birthday !== formatDate) {
		// 校验日期是否合法
		return false;
	}
	for (let i = 0; i < cardid.length - 1; i++) {
		sum += cardid[i] * first[i];
	}
	const result = sum % 11;
	const last = stard[result]; // 计算出来的最后一位身份证号码
	if (cardid[cardid.length - 1].toUpperCase() === last) {
		return true;
	} else {
		return false;
	}
};

/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
	const valid_map = ["admin", "editor"];
	return valid_map.indexOf(str.trim()) >= 0;
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
	const reg = /^[a-z]+$/;
	return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
	const reg = /^[A-Z]+$/;
	return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
	const reg = /^[A-Za-z]+$/;
	return reg.test(str);
}


/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
	if (typeof str === "string" || str instanceof String) {
		return true;
	}
	return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
	if (typeof Array.isArray === "undefined") {
		return Object.prototype.toString.call(arg) === "[object Array]";
	}
	return Array.isArray(arg);
}

// 是否对象
export const isObj = o => {
	return Object.prototype.toString.call(o).slice(8, -1) === "Object";
};

/**
 * 是否合法IP地址
 * @param rule
 * @param value
 * @param callback
 */
export function validateIP(rule, value, callback) {
	if (value === "" || value === undefined || value == null) {
		callback();
	} else {
		const reg =
			/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
		if (!reg.test(value) && value !== "") {
			callback(new Error("请输入正确的IP地址"));
		} else {
			callback();
		}
	}
}

/* 是否手机号码或者固话*/
export function validatePhoneTwo(rule, value, callback) {
	const reg = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/;
	if (value === "" || value === undefined || value == null) {
		callback();
	} else {
		if (!reg.test(value) && value !== "") {
			callback(new Error("请输入正确的电话号码或者固话号码"));
		} else {
			callback();
		}
	}
}

/* 是否固话*/
export function validateTelephone(rule, value, callback) {
	const reg = /0\d{2}-\d{7,8}/;
	if (value === "" || value === undefined || value == null) {
		callback();
	} else {
		if (!reg.test(value) && value !== "") {
			callback(new Error("请输入正确的固话（格式：区号+号码,如010-1234567）"));
		} else {
			callback();
		}
	}
}

/* 是否手机号码*/
export function validatePhone(rule, value, callback) {
	const reg = /^[1][3,4,5,7,8][0-9]{9}$/;
	if (value === "" || value === undefined || value == null) {
		callback();
	} else {
		if (!reg.test(value) && value !== "") {
			callback(new Error("请输入正确的电话号码"));
		} else {
			callback();
		}
	}
}

/* 是否身份证号码*/
export function validateIdNo(rule, value, callback) {
	const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	if (value === "" || value === undefined || value == null) {
		callback();
	} else {
		if (!reg.test(value) && value !== "") {
			callback(new Error("请输入正确的身份证号码"));
		} else {
			callback();
		}
	}
}
